package cn.itsource.basic.service.impl;


import cn.itsource.PageList;
import cn.itsource.basic.mapper.BaseMapper;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.IBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> queryAll() {
        return baseMapper.queryAll();
    }

    @Override
    public T queryOne(Long id) {
        return baseMapper.queryOne(id);
    }

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public PageList queryPage(BaseQuery baseQuery) {
        PageList pageList = new PageList();
        //总数 select count(*) from xxx where ?
        Long total =  baseMapper.queryTotal(baseQuery);
        //select * from xxx where xxx limit
        List rows =  baseMapper.queryData(baseQuery);
        pageList.setTotal(total);
        pageList.setRows(rows);
        return pageList;
    }
}
